# -*- coding:utf8 -*-
#!/usr/bin/env python
import os
import feedparser
import copy
import rfc822
import time
import datetime

from xml.etree import cElementTree as ElementTree
from threading import *

#from datetime import datetime
from django.core.management.base import BaseCommand, CommandError
from django.db.models import Q

from feedsaver.saver.models import News

def crawl(hit_list,subject,self):
    # pull down all feeds
    future_calls = [Future(feedparser.parse, rss_url) for rss_url in hit_list]
    # block until they are all in
    feeds = [future_obj() for future_obj in future_calls]
        
    entries = []
    for feed in feeds:
        self.stdout.write(feed[ "channel" ][ "title" ])
        self.stdout.write("\n") 
            
        for item in feed["items"]:
                
                #First validates if the it's a new news
                updated = datetime.datetime.fromtimestamp(time.mktime(rfc822.parsedate(item["updated"])))
              
                exists = News.objects.filter(Q(news_id=item["id"]) , Q(pub_date__gte=updated)).count()
                             
                if (exists > 0):
                    continue # This news in the db is the latest updated version
                else:
                    # Save the news in db
                
                    #debugging
                    #for i in item.keys():
                        #self.stdout.write(i)
                        #self.stdout.write("\n") 
                    
                    self.stdout.write(item["title"])
                        
                    n = News()
                    n.original = item["summary"] # Original Text
                    n.title = item["title"] # News title
                    n.pub_date = updated # Date updated
                    n.news_id = item["id"]  # News identificator
                    n.url = item["link"] # URL to the original news
                    n.subject = subject # Subject of the News
                    
                    #n.save()
                    
                    self.stdout.write("\n")
                    self.stdout.write("\n") 
                
#                    entries.extend( feed[ "items" ] ) 
#    
#                    sorted_entries = sorted(entries, key=lambda entry: entry["date_parsed"])
#                    sorted_entries = sorted_entries.reverse()
    return


class Command(BaseCommand):
    args = 'no args'
    help = 'Extract info from CPR'
    
    def handle(self, *args, **options):
        self.stdout.write('>> Starting main loop.\n')

        #Brazil News
        

        #World News
        folha_mundo = "http://fulltextrssfeed.com/feeds.folha.uol.com.br/mundo/rss091.xml"

        #Negócios
        folha_mercado = "http://fulltextrssfeed.com/feeds.folha.uol.com.br/mercado/rss091.xml"

        #Science/Tecnology
        folha_ciencia = "http://fulltextrssfeed.com/feeds.folha.uol.com.br/ciencia/rss091.xml"
        folha_tec = "http://fulltextrssfeed.com/feeds.folha.uol.com.br/tec/rss091.xml"

        #Entertainment
        
        #Sports News
        folha_esportes = "http://fulltextrssfeed.com/feeds.folha.uol.com.br/esporte/rss091.xml"
        estadao_esportes = "http://fulltextrssfeed.com/estadao.feedsportal.com/c/33043/f/534114/index.rss"
        terra_esportes = "http://fulltextrssfeed.com/rss.terra.com.br/0,,EI1137,00.xml"
        
        hit_list = [folha_esportes,estadao_esportes]
        
        crawl(hit_list,'es',self)

        #Health
        folha_equilibrio = "http://fulltextrssfeed.com/feeds.folha.uol.com.br/equilibrioesaude/rss091.xml"
       
class Future:

    def __init__(self,func,*param):
        # Constructor
        self.__done=0
        self.__result=None
        self.__status='working'

        self.__C=Condition()   # Notify on this Condition when result is ready

        # Run the actual function in a separate thread
        self.__T=Thread(target=self.Wrapper,args=(func,param))
        self.__T.setName("FutureThread")
        self.__T.start()

    def __repr__(self):
        return '<Future at '+hex(id(self))+':'+self.__status+'>'

    def __call__(self):
        self.__C.acquire()
        while self.__done==0:
            self.__C.wait()
        self.__C.release()
        # We deepcopy __result to prevent accidental tampering with it.
        a=copy.deepcopy(self.__result)
        return a

    def Wrapper(self, func, param):
        # Run the actual function, and let us housekeep around it
        self.__C.acquire()
        try:
            self.__result=func(*param)
        except:
            self.__result="Exception raised within Future"
        self.__done=1
        self.__status=`self.__result`
        self.__C.notify()
        self.__C.release()